/*
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
*/
package cmd

import (
	"barge/app"
	"fmt"
	"github.com/rs/zerolog"
	"github.com/rs/zerolog/log"
	"github.com/spf13/cobra"
)

// pushCmd represents the push command
var pushCmd = &cobra.Command{
	Use:   "push",
	Short: "推送本地镜像到目标仓库",
	Long:  `不拉取镜像, 只有本地镜像存在时，才能推送到目标仓库, 适用于本地构建的镜像推送到目标仓库`,
	Args: func(cmd *cobra.Command, args []string) error {
		if pushOpt.Images == nil {
			pushOpt.Images = args
		}
		if len(pushOpt.Images) == 0 {
			return fmt.Errorf("镜像列表不能为空")
		}
		if pushOpt.Registry == "" {
			return fmt.Errorf("目标仓库不能为空")
		}
		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {
		if runOpt.Quiet {
			zerolog.SetGlobalLevel(zerolog.InfoLevel)
		}
		r, err := app.NewPushRunner(pushOpt)
		if err != nil {
			log.Err(err).Send()
			return
		}
		err = r.Run()
		if err != nil {
			log.Err(err).Send()
			return
		}
	},
}

var pushOpt = new(app.PushOption)

func init() {
	rootCmd.AddCommand(pushCmd)

	pushCmd.Flags().BoolVarP(&pushOpt.Quiet, "quiet", "q", false, "静默模式")
	pushCmd.Flags().StringVarP(&pushOpt.Registry, "registry", "r", "", "镜像仓库名称")
	pushCmd.Flags().StringArrayVarP(&pushOpt.Images, "images", "i", nil, "传输镜像列表")
}
